package com.changpay.zywy.ui.activity.loan

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.LoanInfoAdapter
import com.changpay.zywy.app.App
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.LoanInfoBean
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.activity.freeze.FreezePeopleActivity
import com.changpay.zywy.utils.DialogUtil
import com.changpay.zywy.wight.SpaceItemDecoration
import kotlinx.android.synthetic.main.activity_loan_first.*
import kotlinx.android.synthetic.main.title_layout.*

class LoanFirstActivity : BaseActivity(), IView {

    private var gc_id: String = ""
    private var member_id: String = ""
    private val mDialog by lazy {
        DialogUtil.getWaitDialog(this, "加载中")
    }

    override fun showLoading() {
        mDialog.show()
    }

    override fun hideLoading() {
        mDialog.dismiss()
    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
            R.id.tv_project_name ->
                Intent(this, LoanSecondActivity::class.java).run {
                    startActivityForResult(this, 100)
                }
            R.id.tv_daili_name -> {
                Intent(this, FreezePeopleActivity::class.java).run {
                    putExtra("type",3)
                    startActivityForResult(this, 200)
                }
            }
        }
    }

    private fun submit(gc_id: String, member_id: String) {
        val params = HashMap<String, String>()
        params["key"] = token
        params["client"] = "android"
        params["gc_id"] = gc_id
        params["member_id"] = member_id
        RetrofitHelper.service.get_tBill_list(params).sss(this)
        {
            datas.clear()
            datas.addAll(it.result)
            mAdapter.notifyDataSetChanged()
        }
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_loan_first
    }

    override fun initData() {

    }

    override fun initView() {

        tv_title.text = "货款管理"
        iv_back.setOnClickListener(onClickListener)
        tv_project_name.setOnClickListener(onClickListener)
        tv_daili_name.setOnClickListener(onClickListener)
        tv_daili_phone.setOnClickListener(onClickListener)
        initRecyclerView()
    }

    override fun start() {

    }


    /**
     * datas
     */
    private val datas = mutableListOf<LoanInfoBean>()

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        this.let {
            SpaceItemDecoration(App.context!!)
        }
    }

    /**
     * Adapter
     */
    private val mAdapter: LoanInfoAdapter by lazy {
        LoanInfoAdapter(App.context, datas)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(App.context)
    }


    /**
     * ItemClickListener
     */
    private val ItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        val positionData = datas[position]


        Intent(this, FinishAccountActivity::class.java).run {
            type = "0"
            putExtra("allot_id", positionData.allot_id)
            startActivity(this)
        }

    }


    /**
     * ItemChildClickListener
     */
    private val onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->
        when (view.id) {
        }
    }

    private fun initRecyclerView() {
        recyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
//            itemAnimator = DefaultItemAnimator()
//            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        mAdapter.run {
            bindToRecyclerView(recyclerView)
            onItemChildClickListener = this.onItemChildClickListener
            onItemClickListener = this@LoanFirstActivity.ItemClickListener
            setEmptyView(R.layout.fragment_empty_layout)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 500 && requestCode == 100) {
            tv_project_name.text = data?.getStringExtra("gc_name")
            gc_id = data!!.getStringExtra("gc_id")
            if (member_id.isNotEmpty()) submit(gc_id, member_id)
        }

        if (resultCode == 200 && requestCode == 200) {
            tv_daili_name.text = data?.getStringExtra("member_name")
            tv_daili_phone.text = data?.getStringExtra("member_phone")
            member_id = data!!.getStringExtra("member_id")
            if (gc_id.isNotEmpty()) submit(gc_id, member_id)
        }
    }
}
